package cn.anc.aonicardv.module.ui.album;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.Gravity;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.anc.aonicardv.Constant;
import cn.anc.aonicardv.R;
import cn.anc.aonicardv.R2;
import cn.anc.aonicardv.bean.VideoBean;
import cn.anc.aonicardv.event.PlayVideoDataEvent;
import cn.anc.aonicardv.media.PlayerController;
import cn.anc.aonicardv.media.PlayerVideoView;
import cn.anc.aonicardv.module.ui.base.BaseActivity;
import cn.anc.aonicardv.util.DialogUtils;
import tv.danmaku.ijk.media.player.IMediaPlayer;

/**
 * Created by yangdai on 2017/5/23.
 */

public class PlayVideoActivity extends BaseActivity  {

    @BindView(R2.id.tv_exit)
    TextView tvExit;
    @BindView(R2.id.tv_title_name)
    TextView tvTitle;

    @BindView(R2.id.player)
    PlayerVideoView playerVideoView;

    private List<VideoBean> videoBeanList;
    private int currentPosition;
    private ProgressDialog progressDialog;

    private DialogUtils dialogUtils;
    private PlayerController playerController;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_play_video);
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

    @Override
    protected void onResume() {
        super.onResume();
        playerVideoView.onResume();
    }

    @Override
    public void initVariable() {
        dialogUtils = new DialogUtils();
        progressDialog = dialogUtils.getProgressDialog(this);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setMessage(getString(R.string.recorder_album_loading_data_hint));
        progressDialog.setCancelable(false);
        playerController = new PlayerController(this);
        playerController.setFullScreenBtnVisibility(false);
        ProgressBar bufferingBar = new ProgressBar(this);
        playerVideoView.setMediaController(playerController);
        playerVideoView.setMediaBufferingIndicator(bufferingBar, new FrameLayout.LayoutParams(150, 150, Gravity.CENTER));
    }

    @Override
    public void setListener() {
        playerController.setOnFullScreenClickListener(onFullScreenClickListener);
        playerController.setOnPlayLastClickListener(onPlayLastClickListener);
        playerController.setOnPlayNextClickListener(onPlayNextClickListener);
        playerController.setOnFullScreenClickListener(onFullScreenClickListener);
        playerController.setOnVideoBackListener(onVideoBackListener);
        playerController.setOnPlayStatusListener(onPlayStatusListener);
        playerController.setOnPlayClickListener(playClickListener);
        playerVideoView.setOnCompletionListener(onCompletionListener);
    }

    @Override
    public void initData() {
       }



    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onPause() {
        super.onPause();
        playerVideoView.onPause();
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null!=playerVideoView){
            playerVideoView.releasePlayer();
        }
        EventBus.getDefault().unregister(this);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

    @OnClick(R2.id.tv_exit)
    public void back() {
        finish();
    }


    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onMessageEvent(PlayVideoDataEvent event) {
        videoBeanList = event.bean;
        currentPosition = getIntent().getIntExtra(Constant.IntentKeyParam.POSITION, 0);
        playerVideoView.setDataSource(videoBeanList.get(currentPosition).getPath());
        updateTitleAndTrackAndThumbnail(currentPosition);

    }


    private void updateTitle(int position) {
        String fileCreateTime = videoBeanList.get(position).fileCreateTime;
        if (fileCreateTime != null) {
            String title = fileCreateTime;
            tvTitle.setText(title);
        }

    }





    public void updateTitleAndTrackAndThumbnail(int position) {
        updateTitle(position);
        if (playerVideoView != null) {
            playerVideoView.start();
        }
        playerController.setThumbnail(videoBeanList.get(position).getPath());



    }



    private void resetPlayerAndRefreshUi(int position) {
        playerVideoView.releasePlayer();
        playerVideoView.setDataSource(videoBeanList.get(position).getPath());
        updateTitleAndTrackAndThumbnail(position);
    }


    PlayerController.OnFullScreenClickListener onFullScreenClickListener = new PlayerController.OnFullScreenClickListener() {
        @Override
        public void onFullScreenClick() {
            playerVideoView.fullScreen();
            playerController.setTitle(videoBeanList.get(currentPosition).getName());
        }
    };




    PlayerController.OnPlayLastClickListener onPlayLastClickListener = new PlayerController.OnPlayLastClickListener() {
        @Override
        public void onPlayLastClick() {
            if (currentPosition>0){
                currentPosition --;
                resetPlayerAndRefreshUi(currentPosition);
            }
        }
    };

    PlayerController.OnPlayNextClickListener onPlayNextClickListener = new PlayerController.OnPlayNextClickListener() {
        @Override
        public void onPlayNextClick() {
            if (currentPosition<videoBeanList.size()-1){
                currentPosition ++;
                resetPlayerAndRefreshUi(currentPosition);
            }
        }
    };
    PlayerController.OnVideoBackListener onVideoBackListener = new PlayerController.OnVideoBackListener() {
        @Override
        public void back() {
       finish();
        }
    };

    PlayerController.OnPlayStatusListener onPlayStatusListener = new PlayerController.OnPlayStatusListener() {
        @Override
        public void onPlayStatus(int status) {
            if (status == PlayerController.STATE_PLAYING) {
                playerController.setTitle("");
            }
        }
    };

    PlayerController.OnPlayClickListener playClickListener = new PlayerController.OnPlayClickListener() {
        @Override
        public boolean onPlayClick() {
            return false;
        }
    };

    IMediaPlayer.OnCompletionListener onCompletionListener = new IMediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(IMediaPlayer mp) {
           if (currentPosition<videoBeanList.size()-1){
               currentPosition ++;
               resetPlayerAndRefreshUi(currentPosition);


           }
        }
    };

}
